package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;
import com.afd.ozblip.view.QuickAlphabeticBar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<UserEntity> list;
	private HashMap<String, Integer> alphaIndexer;//
	private String[] sections;//
	private Context ctx;
	private LoadImage loadImage;

	public ContactListAdapter(Context context, List<UserEntity> list,
			QuickAlphabeticBar alpha) {

		this.ctx = context;
		this.inflater = LayoutInflater.from(ctx);
		this.loadImage = new LoadImage(ctx);
		this.list = list;
		this.alphaIndexer = new HashMap<String, Integer>();
		this.sections = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			String name = getAlpha(list.get(i).getUserNickNameJM());
			if (!alphaIndexer.containsKey(name)) {
				alphaIndexer.put(name, i);
			}
		}
		Set<String> sectionLetters = alphaIndexer.keySet();
		ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
		Collections.sort(sectionList);
		sections = new String[sectionList.size()];
		sectionList.toArray(sections);
		alpha.setAlphaIndexer(alphaIndexer);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void remove(int position) {
		list.remove(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.contact_home_list_item,
					null);
			holder = new ViewHolder();
			holder.qcb = (ImageView) convertView
					.findViewById(R.id.iv_contact_item_image);
			holder.alpha = (TextView) convertView
					.findViewById(R.id.tv_contact_item_alpha);
			holder.name = (TextView) convertView
					.findViewById(R.id.tv_contact_item_nickName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		UserEntity cb = list.get(position);
		String name = cb.getUserName();
		holder.name.setText(name);
		// 设置头像
		String headUrl = cb.getUserHeadUrl();
		if (StringUtil.isNullOrEmpty(headUrl)) {
			holder.qcb.setImageResource(R.drawable.default_head_pic);
		} else {
			loadImage.addTask(Constants.WEB_PATH + headUrl, holder.qcb);
		}
		// 获取当前首字母
		String currentStr = getAlpha(cb.getUserNickNameJM());
		//
		String previewStr = (position - 1) >= 0 ? getAlpha(list.get(
				position - 1).getUserNickNameJM()) : " ";
		/**
		 * 设置首字母
		 */
		if (!previewStr.equals(currentStr)) { //
			holder.alpha.setVisibility(View.VISIBLE);
			holder.alpha.setText(currentStr);
		} else {
			holder.alpha.setVisibility(View.GONE);
		}
		return convertView;
	}

	private static class ViewHolder {
		ImageView qcb;
		TextView alpha;
		TextView name;
	}

	/**
	 * 
	 * 获取首字母
	 * 
	 * @param str
	 * @return
	 */
	private String getAlpha(String str) {
		if (str == null) {
			return "#";
		}
		if (str.trim().length() == 0) {
			return "#";
		}
		String c = str.trim().substring(0, 1);
		//
		Pattern pattern = Pattern.compile("^[A-Za-z]+$");
		if (pattern.matcher(c).matches()) {
			return c.toUpperCase(); //
		} else {
			return "#";
		}
	}
}
